<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
		"http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>doh.robot Tree DnD Multi-parent Test</title>

		<style>
			@import "../../../../util/doh/robot/robot.css";
		</style>

		<!-- required: dojo.js -->
		<script type="text/javascript" src="../../../../dojo/dojo.js"
			djConfig="isDebug: true, parseOnLoad: true"></script>

		<!-- functions to help test -->
		<script type="text/javascript" src="../../helpers.js"></script>
		<script type="text/javascript" src="Tree_dnd.js"></script>

		<script type="text/javascript">
			dojo.require("dojo.window");
			dojo.require("dijit.robotx");

			dojo.addOnLoad(function(){
				doh.robot.initRobot('../test_Tree_DnD.html');

				setup();

				doh.register("multi-parent tests", [
					{
						name: "add 'Fruits' as parent of vegetables",
						timeout: 10000,
						runTest: function(){
							var
								d = new doh.Deferred(),
								vegetablesTreeNode = findTreeNodeByPath("collectionsTree", ["Vegetables (0)"]),	// the left tree
								fruitsTreeNode = findTreeNodeByPath("itemTree", ["Fruits"]);	// right tree

							doh.robot.mouseMoveAt("collectionsTree", 500, 1); // scroll source parent into view
							doh.robot.mouseMoveAt(vegetablesTreeNode.domNode, 500, 1);
							// Drag and Drop, *copying* in order to add a parent rather than change
							doh.robot.keyDown(dojo.keys.copyKey, 500);
							if(dojo.isMac){
								// I can't get DOH robot to generate the mousePress event w/metaKey=true so
								// hack it
								doh.robot.sequence(function(){
									realIsCopyKey = dojo.global.dojo.isCopyKey;
									dojo.global.dojo.isCopyKey = function(){ return true; }
								});
							}

							doh.robot.mousePress({left: true}, 500);
							doh.robot.mouseMoveAt("itemTree", 500, 500); // move to dest parent to scroll it into view
							doh.robot.mouseMoveAt(fruitsTreeNode.labelNode, 0, 500);
							doh.robot.mouseRelease({left: true}, 500);
							doh.robot.keyUp(dojo.keys.copyKey, 500);
							if(dojo.isMac){
								// See if(dojo.isMac) above
								doh.robot.sequence(function(){
									dojo.global.dojo.isCopyKey = realIsCopyKey;
								});
							}

							doh.robot.sequence(d.getTestCallback(function(){
								// Check that Vegetables remains a child of Foods
								var children = getNamesOfChildrenOfItem("Foods");
								doh.is(3, children.categories.length, "foods category child");
								doh.is("Fruits", children.categories[0]);
								doh.is("Vegetables", children.categories[1]);
								doh.is("Cereals", children.categories[2]);

								// Check that Vegetables added as child of Fruits
								var children = getNamesOfChildrenOfItem("Fruits");
								doh.is(2, children.categories.length, "foods category child");
								doh.is("Citrus", children.categories[0]);
								doh.is("Vegetables", children.categories[1]);

								// Check that new item *wasn't* created
								var items;
								dojo.global.myStore.fetch({
									onComplete: function(i){ items = i; }
								});
								doh.is(6, items.length, "# of items in store");

								// Check that data store update was reflected in the tree
								doh.t(fruitsTreeNode.isExpanded, "drop caused the node to expand");
								var treeNodeChildren = fruitsTreeNode.getChildren();
								doh.is(2, treeNodeChildren.length, "2 TreeNode children")
								doh.is("Citrus", innerText(treeNodeChildren[0].labelNode));
								doh.is("Vegetables", innerText(treeNodeChildren[1].labelNode));
							}), 1000);	// 1000ms to wait for 'Fruits' node to expand and show 'Apple' node

							return d;
						}
					},
					{
						name: "move Foods/Vegetables to Foods/Cereal/Vegetables",
						timeout: 10000,
						runTest: function(){
							var
								d = new doh.Deferred(),
								vegetablesTreeNode = findTreeNodeByPath("itemTree", ["Vegetables"]),
								cerealsTreeNode = findTreeNodeByPath("itemTree", ["Cereals"]);

							dojo.global.scrollTo(0, 9000); // scroll to bottom to try and prevent the DnD code from autoscrolling

							// Drag and Drop
							doh.robot.mouseMoveAt(vegetablesTreeNode.domNode, 500, 1);
							doh.robot.mouseClick({left: true}, 500); // helps prevent screen jitter
							doh.robot.mousePress({left: true}, 1000);
							doh.robot.mouseMoveAt(vegetablesTreeNode.labelNode, 500, 500, 0, 0); // move mouse slightly to trigger DnD
							doh.robot.mouseMoveAt(cerealsTreeNode.domNode, 500, 100); // double move to allow for autoscrolling jitter
							doh.robot.mouseMoveAt(cerealsTreeNode.domNode, 500, 100);
							doh.robot.mouseRelease({left: true}, 500);

							doh.robot.sequence(d.getTestCallback(function(){
								// Check that Vegetables remains a child of Fruits
								var children = getNamesOfChildrenOfItem("Fruits");
								doh.is(2, children.categories.length, "fruits category child");
								doh.is("Citrus", children.categories[0]);
								doh.is("Vegetables", children.categories[1]);

								// Check that Vegetables added as child of Cereals
								children = getNamesOfChildrenOfItem("Cereals");
								doh.is(1, children.categories.length, "cereals category child");
								doh.is("Vegetables", children.categories[0]);

								// Check that Vegetables no longer a child of Foods
								children = getNamesOfChildrenOfItem("Foods");
								doh.is(2, children.categories.length, "foods category child");
								doh.is("Fruits", children.categories[0]);
								doh.is("Cereals", children.categories[1]);

								// Check that new item *wasn't* created
								var items;
								dojo.global.myStore.fetch({
									onComplete: function(i){ items = i; }
								});
								doh.is(6, items.length, "# of items in store");
							}), 1000);	// 1000ms to wait for 'Fruits' node to expand and show 'Apple' node

							return d;
						}
					},
					{
						name: "move Fruits/Vegetables to Fruits/Citrus",
						timeout: 10000,
						runTest: function(){
							var
								d = new doh.Deferred(),
								vegetablesTreeNode = findTreeNodeByPath("itemTree", ["Fruits", "Vegetables"]),
								citrusTreeNode = findTreeNodeByPath("itemTree", ["Fruits", "Citrus"]);

							// Drag and Drop
							doh.robot.mouseMoveAt(vegetablesTreeNode.labelNode, 500, 1);
							doh.robot.mouseClick({left: true}, 500); // helps prevent screen jitter
							doh.robot.mousePress({left: true}, 1000);
							doh.robot.mouseMoveAt(vegetablesTreeNode.labelNode, 500, 500, 0, 0); // move mouse slightly to trigger DnD
							doh.robot.mouseMoveAt(citrusTreeNode.labelNode, 0, 500);
							doh.robot.mouseRelease({left: true}, 500);

							doh.robot.sequence(d.getTestCallback(function(){
								// Check that Vegetables remains a child of Cereals
								var children = getNamesOfChildrenOfItem("Cereals");
								doh.is(1, children.categories.length, "cereals category child");
								doh.is("Vegetables", children.categories[0]);

								// Check that Vegetables added as child of Citrus
								children = getNamesOfChildrenOfItem("Citrus");
								doh.is(1, children.categories.length, "citrus category child");
								doh.is("Vegetables", children.categories[0]);
								doh.is(1, children.items.length, "citrus item child");
								doh.is("Orange", children.items[0]);

								// Check that Vegetables removed as child of Fruits
								children = getNamesOfChildrenOfItem("Fruits");
								doh.is(1, children.categories.length, "fruits category child");
								doh.is("Citrus", children.categories[0]);

								// Check that new item *wasn't* created
								var items;
								dojo.global.myStore.fetch({
									onComplete: function(i){ items = i; }
								});
								doh.is(6, items.length, "# of items in store");
							}), 1000);	// 1000ms to wait for 'Fruits' node to expand and show 'Apple' node

							return d;
						}
					}
				]);

				doh.register("external drop tests", [
					{
						name: "drop banana on fruits",
						timeout: 10000,
						runTest: function(){
							var
								d = new doh.Deferred(),
								fruitsTreeNode = findTreeNodeByPath("itemTree", ["Fruits"]);	// right tree

							doh.robot.mouseMoveAt(dojo.byId("1003").parentNode, 500, 1, 0, 0); // move to source parent node to help prevent DnD autoscrolling
							doh.robot.mouseMoveAt(dojo.byId("1003"), 500, 1); // move to source banana node
							doh.robot.mousePress({left: true}, 500);
							doh.robot.mouseMoveAt(dojo.byId("1003").parentNode, 500, 100); //  move just a little to start the DnD operation before scrolling
							doh.robot.mouseMoveAt("itemTree", 500, 500); // go to target parent top to allow for better scrolling of the destination
							doh.robot.mouseMoveAt(fruitsTreeNode.labelNode, 1000, 500);
							doh.robot.mouseRelease({left: true}, 500);

							doh.robot.sequence(d.getTestCallback(function(){

								var children = getChildrenOfItem("Fruits");
								doh.is(1, children.items.length, "new item added");
								doh.is(1003, children.items[0].id, "id of new item came from dropped node");
								doh.is("Banana", children.items[0].name, "name of new item came from dropped node");

								dojo.global.myStore.fetch({
									query: { name: "Banana" },
									queryOptions: { deep: true },
									onComplete: function(i){ items = i; }
								});
								doh.is(1, items.length, "1 banana item in store");
							}), 1000);

							return d;
						}
					},
					{
						name: "drop banana on cereal",
						timeout: 10000,
						runTest: function(){
							var
								d = new doh.Deferred(),
								cerealsTreeNode = findTreeNodeByPath("itemTree", ["Cereals"]);					// right tree

							doh.robot.mouseMoveAt(dojo.byId("1003").parentNode, 500, 1, 0, 0); // move to source parent node to help prevent DnD autoscrolling
							doh.robot.mouseMoveAt(dojo.byId("1003"), 500, 1); // move to source banana node
							doh.robot.mousePress({left: true}, 500);
							doh.robot.mouseMoveAt(dojo.byId("1003").parentNode, 500, 100); //  move just a little to start the DnD operation before scrolling
							doh.robot.mouseMoveAt("itemTree", 500, 100); // go to target parent top to allow for better scrolling of the destination
							doh.robot.mouseMoveAt(cerealsTreeNode.labelNode, 1000, 500);
							doh.robot.mouseRelease({left: true}, 500);

							doh.robot.sequence(d.getTestCallback(function(){

								var children = getChildrenOfItem("Cereals");
								doh.is(1, children.items.length, "banana is child of cereals");
								doh.is(1003, children.items[0].id, "id of item came from dropped node");
								doh.is("Banana", children.items[0].name, "name of new item came from dropped node");

								var children = getChildrenOfItem("Fruits");
								doh.is(1, children.items.length, "banana still a child of fruits");
								doh.is(1003, children.items[0].id, "id of new item came from dropped node");
								doh.is("Banana", children.items[0].name, "name of new item came from dropped node");

								dojo.global.myStore.fetch({
									query: { name: "Banana" },
									queryOptions: { deep: true },
									onComplete: function(i){ items = i; }
								});
								doh.is(1, items.length, "1 banana item in store (with 2 parents)");
							}), 1000);

							return d;
						}
					}
				]);

				doh.run();
			});
		</script>
	</head>
</html>
